/**
 * 
 */
package com.mnurre.shopping.assistant;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.SimpleCursorAdapter;

/**
 * @author michael
 *
 */
public class ManageListsActivity extends ListActivity {

	private static final String TAG = "ManageListsActivity";
	
	private ListsData mData;
	private Cursor mCursor;
	
	private static final int LIST_ADD = 0;
	private static final int LIST_EDIT = 1;
	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Log.d(TAG, "Creating activity.");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_manage_lists);

		Log.d(TAG, "Creating a new ListsData.");
		mData = new ListsData(this);
		
		Log.d(TAG, "Populating lists.");
		populateLists();
		
		Log.d(TAG, "Registering for content menu.");
		registerForContextMenu(getListView());
		
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		Log.d(TAG, "Creating options menu.");
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_manage, menu);
		Log.d(TAG, "Done creating options menu.");
		return true;
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		Log.d(TAG, "Creating context menu.");
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_manage_context, menu);
		Log.d(TAG, "Done creating context menu.");
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onMenuItemSelected(int, android.view.MenuItem)
	 */
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
		case R.id.menu_manage_add:
			// start the EditListActivity for result
			Log.d(TAG, "The add menu was selected. Starting intent to add list.");
			Intent intent = new Intent(this, EditListActivity.class);
			startActivityForResult(intent, LIST_ADD);
			break;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		Log.d(TAG, "Context menu item selected.");
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
		switch(item.getItemId()){
		case R.id.manage_menu_context_option_delete:
			Log.d(TAG, "Delete context menu selected.");
			// remove all products from the list and then delete the list
			mData.open();
			mData.removeList(info.id);
			mData.close();
			populateLists();
			break;
		case R.id.manage_menu_context_option_edit:
			Log.d(TAG, "Edit context menu selected.");
			Intent intent = new Intent(this, EditListActivity.class);
			intent.putExtra(DataManager.Tables.Lists.COLUMN_ID, info.id);
			startActivityForResult(intent, LIST_EDIT);
			break;
		}
		return super.onContextItemSelected(item);
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		populateLists();
	}
	
	/*
	 * Populates the UI with the list information
	 */
	private void populateLists(){
		Log.d(TAG, "populateLists called.");
		Log.d(TAG, "Opening data and getting a cursor for all lists.");
		mData.open();
		mCursor = mData.getAllLists();

		Log.d(TAG, "Calling startManagingCursor.");
		startManagingCursor(mCursor);

		Log.d(TAG, "Creating from array.");
		String[] from = new String[] { DataManager.Tables.Lists.COLUMN_NAME };
		
		Log.d(TAG, "Creating to array.");
		int[] to = new int[] { android.R.id.text1 };
		
		Log.d(TAG, "Creating a new SimpleCursorAdapter.");
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, mCursor, from, to);
		
		Log.d(TAG, "Calling setListAdapter.");
		setListAdapter(adapter);
		
		Log.d(TAG, "Closing database connection.");
		mData.close();
	}

}
